from cm_api.api_client import ApiResource
import requests
import json

# # possible status:
# #   UNKNOWN
# #   NONE
# #   STOPPED
# #   DOWN
# #   UNKNOWN_HEALTH
# #   DISABLED_HEALTH
# #   CONCERNING_HEALTH
# #   BAD_HEALTH
# #   GOOD_HEALTH
# #   STARTING
# #   STOPPING
# #   HISTORY_NOT_AVAILABLE
#
# version = 12
# cluster_name = "Cluster 1"
#
# # ***online***
# cm_host = "172.19.100.12"
# username = "dp"
# password = "dp_ppc"
# kafka_brokers = {'kafka-KAFKA_BROKER-689e43e63563a6772803dceae726f054': 'hadoop20.bs.ppcredit.com',
#                  'kafka-KAFKA_BROKER-b3802d188bf789813cc723c0d80617f8': 'hadoop28.bs.ppcredit.com',
#                  'kafka-KAFKA_BROKER-b954f61ef4f1ff42b0668001f103833e': 'hadoop19.bs.ppcredit.com'}
#
#
# # ***offline***
# # cm_host = "192.168.213.23"
# # username = "shen"
# # password = "shen"
# # kafka_brokers = {'kafka-KAFKA_BROKER-1aeddd6569c48cbf4de64dcc718220ef': 'hadoop26.sc.ppcredit.com',
# #                  'kafka-KAFKA_BROKER-6da0128f4b8fae8dcf74f30bd593ecfe': 'hadoop25.sc.ppcredit.com',
# #                  'kafka-KAFKA_BROKER-68bebe6cdfb2bb675506f86c8465d64c': 'hadoop31.sc.ppcredit.com'}
#
#
# def send_msg_to_wechat(msg):
#     datas = '{"toparty": 5, "msgtype": "text", "agentid": 1, "text": {"content": "%s"}, "safe": 0}' % msg
#     url_token = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wx9c595e2ff0429f13&corpsecret=0muEXbvPoOZeR_o7hkd3fdt5vBh5o0-uFIKgl4LDRzA-jBTXx7qgKtRO668D-VT6'
#     url_send = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s'
#     resp = requests.get(url_token)
#     access_token = json.loads(resp.content)['access_token']
#     header = {'Accept': 'application/json'}
#     resp = requests.post(url_send % access_token, data=datas, headers=header)
#     print resp.content
#
#
# def get_cdh():
#     api = ApiResource(cm_host, username=username, password=password, version=version)
#     cdh5 = api.get_cluster(cluster_name)
#     return cdh5
#
#
# def restart_kafka_error_status():
#     cdh5 = get_cdh()
#     kafka = cdh5.get_service('kafka')
#     roles = kafka.get_all_roles()
#     error = []
#     for role in roles:
#         status = role.entityStatus
#         if status not in ['GOOD_HEALTH', 'STARTING', 'CONCERNING_HEALTH']:
#             error.append(role.name)
#         else:
#             print 'kafka:', kafka_brokers[role.name], 'status ok!'
#     for name in error:
#         msg = 'restarting kafka broker at %s' % kafka_brokers[name]
#         kafka.restart_roles(name)
#         send_msg_to_wechat(msg)
#
#
# def restart_hbase_error_status():
#     cdh5 = get_cdh()
#     hbase = cdh5.get_service('hbase')
#     roles = hbase.get_all_roles()
#     error = []
#     for role in roles:
#         if role.type != "REGIONSERVER":
#             continue
#         status = role.entityStatus
#         if status not in ['GOOD_HEALTH', 'STARTING', 'CONCERNING_HEALTH']:
#             error.append(role.name)
#         else:
#             print 'hbase:', role.name, 'status ok!'
#     for name in error:
#         msg = 'restarting hbase region server at %s' % name
#         hbase.restart_roles(name)
#         send_msg_to_wechat(msg)
#
#
# restart_hbase_error_status()
# restart_kafka_error_status()

import codecs


f = codecs.open('xxx.txt', 'r','utf-8')
f1 = codecs.open('xxx5.txt', 'r','utf-8')
f2 = codecs.open('xxxa.txt', 'a+','utf-8')
a,b,c,d,e,g=[],[],[],[],[],[]
for line in f:
    r=line.strip().split('\001')
    a.append(r[0])
    b.append(r[1])
    c.append(r[2])
    d.append(r[3])
    e.append(r[4])
for line in f1:
    r = line.strip()
    g.append(r)

for i in range(len(a)):
    f2.write(a[i]+'\001'+b[i]+'\001'+c[i]+'\001'+d[i]+'\001'+e[i]+'\001'+g[i]+'\n')
